use std::io;
use rand::Rng;
use std::cmp::Ordering;
fn demo1 () {
    let secret_number = rand::thread_rng().gen_range(1, 101);
    // println!("The secret number is {}", secret_number);
    println!("Welcome to 'Guess The Number'!");
    loop {
        println!("Please input your guess:");
        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("Failed to read line");
        let guess: u32 = match guess.trim().parse(){
            Ok(num) => num,
            Err(_) => {
                println!("Please type a number, sb!");
                continue
            },
        };
        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!"); break;
            }
        }
    }
}
fn demo2 () {
    let x = 5;
    println!("The value of x is: {}", x);
    // x = 6; 不能改变
    let mut y = 3;
    println!("The value of y is: {}", y);
    y = 7; // mut修饰可以改变
    println!("The value of y is: {}", y);
    let y = 99; // 隐藏原来的变量y，重新声明一个y
    println!("The value of y is: {}", y);
    let a:u8 = 100;
    println!("The value of x is: {}", a);
    let b = 2.0; // 默认推导类型为f64
    let c:f32 = 3.0; // 显式声明类型为32
    println!("The value of b is: {}", b);
    println!("The value of c is: {}", c);
    let d = 'l';
    println!("The value of d is: {}", d);
    let e = "lly";
    println!("The value of e is: {}", e);

    let tub = (99, "qmy", 20.00); // 元祖
    let (e1, e2, e3) = tub;  // 解构
    println!("The value of e1 is: {}", e1);
    println!("The value of e2 is: {}", e2);
    println!("The value of e3 is: {}", e3);

    let arr = [1,2,3,4,5]; // 定义数组
    println!("The value of arr is: {:?}", arr);
}
// 摄氏度和华氏度的转换
fn demo3 (temperature: f32, unit: &str) -> () {
    let result;
    if unit =="摄氏度" {
        result = temperature * 1.8 + 32.0;
    } else {
        result = (temperature - 32.0) / 1.8;
    }
    println!("The value of temperature is: {}", result);
}
// 输出第n个斐波那契数
// 0 1 1 2 3 5 8...
fn demo4 (n: i32) -> (i32) {
    if n < 2 {
        return 0;
    }
    if n == 2 {
        return 1;
    }
    demo4(n- 1) + demo4(n - 2)
}
// 输出前n项斐波那契数
fn demo5(numbber: i32) -> () {
    let mut n = 1;
    while n < numbber {
        print!("{} ",demo4(n));
        n += 1;
    }
}
fn main() {
    demo5(20);
}
